package com.cea.papyrus.core.treeeditpart.diagramview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.resource.Resource;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.listener.ObjectListener;
import com.cea.papyrus.core.treeeditpart.CommonTreeEditPart;
import com.cea.papyrus.core.utils.Debug;
/**
 * this class is used to be the root editpart of the list of diagram.
 * @author Patrick Tessier
 */
public class DiagramListRootEditPart  extends CommonTreeEditPart{

	/**
	 * the channel 20
	 */
	private int channel = 20;
	/**
	 * the Di2 resource that we listen
	 */
	private Resource di2Resource=null;
	/**
	 * the listener in order to update this
	 */
	private ObjectListener resourcetListener= null;

	/**
	 * Create a new instance of this edit part using the given model element.
	 * 
	 * @param element a non-null ShapesDiagram instance
	 */
	public DiagramListRootEditPart(Resource ressource,ModelManager element) {
		super(element, element);
		Debug.debug(this,"ModelManagerEditPartCreate!", channel);
		di2Resource=ressource;
		resourcetListener= new ObjectListener(this);
	}

/**
 * {@inheritDoc}
 */
	@Override
	public void activate() {
		if (!isActive()) {
			super.activate();
			// ad the listener
			((Notifier)di2Resource).eAdapters().add(resourcetListener);
		}
	}
/**
 * {@inheritDoc}
 */
	@Override
	public void deactivate() {
		if (isActive()) {
			super.deactivate();
			// remove the listener
			((Notifier)di2Resource).eAdapters().remove(resourcetListener);
		}
	}

	/**
	 * {@inheritedDoc}
	 */
	@Override
	public List getModelChildren() {
//		the list of all diagrams is all node of the Di2 resource
		ArrayList contained=new ArrayList();
		contained.addAll(di2Resource.getContents());
		// order the collection according to the given Comparator
		Comparator sorter = modelManager.getOutlineSorter();
		if(sorter!=null) {
			Collections.sort(contained, sorter);
		}
		return contained;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ModelManager getModelManager(){
		return modelManager;
	}

}